<?php
/**
 * Created by lni_wang.
 * Product: PhpStorm
 * Date: 2017/4/18 19:42
 * File: common_helper.php
 */
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 返回允许的参数
 *
 * @param   mixed $params 要过滤的参数
 * @param   mixed $params 允许的键名
 *
 * @return  void
 */
if (!function_exists('data_picker')){
    function data_picker(&$params, $allow){
        foreach ($params as $key => $item){
            if (!in_array($key, $allow)){
                unset($params[$key]);
            }else{
                if (is_array($item)){
                    data_filter($item);
                    $params[$key] = $item;
                }else{
                    if($item === null){
                        unset($params[$key]);
                    }else{
                        $params[$key] = trim($item);
                    }
                }
            }
        }
    }
}

/**
 * 对参数进行递归调用
 *
 * @param   string $callback 要调用的函数
 * @param   mixed  $params   要递归的参数
 *
 * @return  void
 */
if (!function_exists('array_map_recursive')){
    function array_map_recursive($callback, &$params){
        if (is_callable($callback)){
            if (is_string($params)){
                $params = call_user_func($callback, $params);
            }else if (is_array($params)){
                foreach ($params as $key => $val){
                    array_map_recursive($callback, $params[$key]);
                }
            }
        }
    }
}

/**
 * 对参数进行过滤
 *
 * @param   mixed $params 要过滤的参数
 *
 * @return  void
 */
if (!function_exists('data_filter')){
    function data_filter(&$params){
        array_map_recursive('strip_tags', $params);
        array_map_recursive('htmlspecialchars', $params);
        array_map_recursive('trim', $params);
    }
}

/**
 * 数据库连接
 * @param $config
 *
 * @return mixed
 */
function connect_db($config){
    static $db = [];
    $key = md5(json_encode($config));
    if (isset($db[$key])){
        return $db[$key];
    }

    $CI       =& get_instance();
    $db[$key] = $CI->load->database($config, true);

    return $db[$key];
}

/**
 *判断是否为url 地址
 * @param $url
 *
 * @return bool
 */
function is_url($url){
    if(strpos($url, 'mailto:' ) !== false){
        return false;
    }else if(strpos($url, 'javascript:') !== false){
        return false;
    }
    $regex = "((https?|ftp)\:\/\/)?"; // SCHEME
    $regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass
    $regex .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP
    $regex .= "(\:[0-9]{2,5})?"; // Port
    $regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path
    $regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query
    $regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor

    if(preg_match("/^$regex$/i", $url)){
        return true;
    }else{
        return false;
    }
}

function explode_trim($str, $ep = ' '){
    if (!is_string($str)){
        return false;
    }

    if (empty($str)){
        return [];
    }

    $_arr = explode($ep, $str);

    $_arr_new = [];
    foreach ($_arr as $_item){
        $_item = trim($_item);
        if (!empty($_item)){
            $_arr_new[] = $_item;
        }
    }

    return $_arr_new;
}

function url_redirect($url){
    header('Location: ' . $url);
    exit;
}

function get_unique_key(){
    return bin2hex(openssl_random_pseudo_bytes(16));
}

function get_url_host($url)
{
    if(!is_url($url)){
        return false;
    }
    $site = parse_url($url);
    if(!isset($site['host']) || !isset($site['scheme'])){
        return false;
    }

    if(!in_array($site['scheme'], ['https', 'http'])){
        return false;
    }

    $host = $site['scheme'].'://'.$site['host'];
    if(isset($site['port'])){
        if($site['scheme'] == 'https' && $site['port'] != 443){
            $host .= ':'.$site['port'];
        }else if($site['scheme'] == 'http' && $site['port'] != 80){
            $host .= ':'.$site['port'];
        }
    }

    return $host;
}
